package com.lagou.pojo;

/**
 * 自定义异常类
 */
public class ErrorContext {

    private static final ThreadLocal<ErrorContext> LOCAL = new ThreadLocal<ErrorContext>();

    /**
     * statementId
     */
    private String statementId;
    private String message;
    private String sql;
    private Throwable cause;

    public static ErrorContext instance() {
        ErrorContext context = LOCAL.get();
        if (context == null) {
            context = new ErrorContext();
            LOCAL.set(context);
        }
        return context;
    }

    public ErrorContext reset() {
        statementId = null;
        message = null;
        sql = null;
        cause = null;
        LOCAL.remove();
        return this;
    }

    public ErrorContext id(String id){
        this.statementId = id;
        return this;
    }

    public ErrorContext message(String message){
        this.message = message;
        return this;
    }

    public ErrorContext sql(String sql){
        this.sql = sql;
        return this;
    }

    public ErrorContext cause(Throwable cause){
        this.cause = cause;
        return this;
    }

    public String toString() {
        StringBuilder description = new StringBuilder();
        description.append("### The error may exist in : ");
        description.append(message);
        if (statementId != null) {
            description.append("\r\n");
            description.append("### Error statementId : " + statementId);
        }
        if (sql != null) {
            description.append("\r\n");
            description.append("### You have an error in your SQL : " + sql);
        }
        if (cause != null){
            description.append("\r\n");
            description.append(cause.toString());
        }
        return description.toString();
    }

}
